package zpy.servlet.servlet;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

public class HttpServletTestServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("GET");
		String name = req.getParameter("name");
		String num = req.getParameter("num");
		req.setAttribute("method", "GET");
		req.setAttribute("name", name);
		req.setAttribute("num", num);
		ServletContext applicaton = getServletContext();
		RequestDispatcher dispatcher =
				applicaton.getRequestDispatcher("/WEB-INF/jsp/httpServletTestResult.jsp");
		dispatcher.forward(req, resp);
	}

	@Override
	protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf-8");
		String name = req.getParameter("name");
		String num = req.getParameter("num");
		
		req.setAttribute("method", "POST");
		req.setAttribute("name", name);
		req.setAttribute("num", num);
		ServletContext applicaton = getServletContext();
		RequestDispatcher dispatcher = 
				applicaton.getRequestDispatcher("/WEB-INF/jsp/httpServletTestResult.jsp");
		dispatcher.forward(req, resp);
	}

	@Override
	protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		System.out.println("put");
		String name = req.getParameter("name");
		String num = req.getParameter("num");
		
		req.setAttribute("method", "PUT");
		req.setAttribute("name", name);
		req.setAttribute("num", num);
		ServletContext applicaton = getServletContext();
		RequestDispatcher dispatcher = 
				applicaton.getRequestDispatcher("/WEB-INF/jsp/httpServletTestResult.jsp");
		dispatcher.forward(req, resp);
	}

	@Override
	protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("delete");
		String name = req.getParameter("name");
		String num = req.getParameter("num");
		
		req.setAttribute("method", "DELETE");
		req.setAttribute("name", name);
		req.setAttribute("num", num);
		ServletContext applicaton = getServletContext();
		RequestDispatcher dispatcher = 
				applicaton.getRequestDispatcher("/WEB-INF/jsp/httpServletTestResult.jsp");
		dispatcher.forward(req, resp);
	}

	@Override
	protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

}
